package com.estore.action;

import java.util.List;

import com.estore.entity.Account;
import com.estore.info.ProfileInfo;
import com.estore.manager.ProfileManager;
import com.estore.model.ProfileModel;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class ProfileAction extends ActionSupport implements
		ModelDriven<ProfileModel> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1305043957158350754L;
	ProfileModel profileModel = new ProfileModel();
	ProfileManager profileManager;

	// update role
	public String updateRole() {
		if (profileModel.getUserID() != null) {
			profileManager.updateRole(profileModel.getUserID());
		}
		return SUCCESS;
	}

	// edit UserProfile
	public String editProfile() {
		profileManager.editProfileInfo(profileModel.getUserID(),
				profileModel.getLastName(), profileModel.getFirstName(),
				profileModel.getEmail(), profileModel.getDateOfBirth(),
				profileModel.getSex(), profileModel.getAddress(),
				profileModel.getPhoneNumber());
		return "success";
	}

	// show Profile
	public String showProfileByID() {
		ProfileInfo info = profileManager.getProfileInfo(profileModel
				.getUserID());
		Account account = profileManager.getAccountInfo(profileModel
				.getUserID());
		if (info != null) {
			profileModel.setProfileInfo(info);
			profileModel.setFirstName(info.getFirstName());
			profileModel.setLastName(info.getLastName());
			profileModel.setEmail(info.getEmail());
			profileModel.setDateOfBirth(info.getDateOfBirth());
			profileModel.setSex(info.getSex());
			profileModel.setAddress(info.getAddress());
			profileModel.setPhoneNumber(info.getPhoneNumber());
			profileModel.setAccount(account.getAccount());
			return "success";
		} else
			return "error";
	}

	// list all profile
	public String listAllProfile() {
		List<ProfileInfo> listprofileinfo = profileManager.getListProfileInfo();
		if (listprofileinfo != null) {
			profileModel.setListprofileinfo(listprofileinfo);
		}
		return "success";
	}

	@Override
	public ProfileModel getModel() {
		// TODO Auto-generated method stub
		return profileModel;
	}

	public ProfileModel getProfileModel() {
		return profileModel;
	}

	public void setProfileModel(ProfileModel profileModel) {
		this.profileModel = profileModel;
	}

	public ProfileManager getProfileManager() {
		return profileManager;
	}

	public void setProfileManager(ProfileManager profileManager) {
		this.profileManager = profileManager;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

}
